package com.chexiehui.jc.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseActivity;
import com.chexiehui.jc.entity.BrandInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.entity.ForumInfo;
import com.chexiehui.jc.listener.OnForumChooseListener;
import com.chexiehui.jc.ui.fragment.ChooseCarForumFragment;
import com.chexiehui.jc.ui.fragment.ChooseCarModelBrandFragment;
import com.chexiehui.jc.ui.fragment.ChooseCarModelSeriesFragment;
import com.chexiehui.jc.ui.fragment.ChooseFroumAreaFragment;
import com.chexiehui.jc.ui.fragment.ChooseFroumMotifFragment;

import butterknife.BindView;

/**
 * 选择品牌 车系 论坛
 */
public class ChooseForumActivity extends BaseActivity implements
        ChooseCarModelBrandFragment.OnBrandChooseListener, ChooseCarModelSeriesFragment.OnSeriesChooseListener,
        OnForumChooseListener {

    @BindView(R.id.fl_choose_page)
    FrameLayout flChoosePage;

    private BrandInfo brandInfo;
    private CarSeriesInfo carSeriesInfo;
    private ForumInfo forumInfo;
    private int chooseType;

    @Override
    protected void beforeSetView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            chooseType = bundle.getInt("chooseType");
        }
    }

    @Override
    protected void setTitleBar() {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_choose_forum;
    }

    @Override
    protected void initView(Bundle var1) {
        if (chooseType == 0) {
            transactionToBrand();
        } else if (chooseType == 1) {
            transactionToArea();
        }else if (chooseType == 2) {
            transactionToMotif();
        }

    }

    private void transactionToMotif() {
        titleBar.setTitleMainText("选择主题论坛");
        ChooseFroumMotifFragment fragment = ChooseFroumMotifFragment.getInstance();
        fragment.setOnChooseListener(this);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_choose_page, fragment);
        transaction.commit();
    }

    private void transactionToArea() {
        titleBar.setTitleMainText("选择区域论坛");
        ChooseFroumAreaFragment fragment = ChooseFroumAreaFragment.getInstance();
        fragment.setOnChooseListener(this);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_choose_page, fragment);
        transaction.commit();
    }


    private void transactionToBrand() {
        titleBar.setTitleMainText("品牌");
        ChooseCarModelBrandFragment fragment = ChooseCarModelBrandFragment.getInstance();
        fragment.setOnChooseListener(this);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_choose_page, fragment);
        transaction.commit();
    }

    private void transactionToSeries() {
        titleBar.setTitleMainText("车系");
        ChooseCarModelSeriesFragment fragment = ChooseCarModelSeriesFragment.getInstance(this.brandInfo);
        fragment.setOnChooseListener(this);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_choose_page, fragment);
        transaction.commit();
    }

    private void transactionToCar() {
        titleBar.setTitleMainText("选择论坛");
        ChooseCarForumFragment fragment = ChooseCarForumFragment.getInstance(this.carSeriesInfo);
        fragment.setOnChooseListener(this);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_choose_page, fragment);
        transaction.commit();
    }

    @Override
    public void onBrandChoose(BrandInfo brandInfo) {
        this.brandInfo = brandInfo;
        transactionToSeries();
    }

    @Override
    public void onSeriesChoose(CarSeriesInfo carSeriesInfo) {
        this.carSeriesInfo = carSeriesInfo;
        transactionToCar();
    }

    @Override
    public void onForumChoose(ForumInfo forumInfo) {
        this.forumInfo = forumInfo;
        Intent data = new Intent();
        data.putExtra("brandInfo", brandInfo);
        data.putExtra("seriesInfo", carSeriesInfo);
        data.putExtra("forumInfo", forumInfo);
        setResult(RESULT_OK, data);
        finish();
    }
}
